package com.xlyy.modbus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xlyy.modbus.bo.AvgDataBo;
import com.xlyy.modbus.entity.Categorize;
import com.xlyy.modbus.entity.MyData;
import com.xlyy.modbus.mapper.MyDataMapper;
import com.xlyy.modbus.service.IMyDataService;
import com.xlyy.modbus.utils.fn.asserts.Vsserts;
import com.xlyy.modbus.vo.MyDataVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoucanji
 * @since 2024-04-28
 */
@Service
public class MyDataServiceImpl extends ServiceImpl<MyDataMapper, MyData>  implements IMyDataService{

    @Override
    public List<MyData> list(MyDataVO myDataVO) {
        LambdaQueryWrapper<MyData> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.between(Vsserts.isNotEmpty(myDataVO.getStartTime()),MyData::getCrateTime,myDataVO.getStartTime(),myDataVO.getEndTime());
        lambdaQueryWrapper.eq(Vsserts.isNotEmpty(myDataVO.getCategorize()), MyData::getCategorize,myDataVO.getCategorize());
        lambdaQueryWrapper.orderByDesc(MyData::getId);
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public List<AvgDataBo> avgDataBoList(String pch) {
        List<AvgDataBo> avgDataBoList=new ArrayList<>();
        List<AvgDataBo> dbAvgDataBoList = this.baseMapper.avgDataBoList(pch);
        if(dbAvgDataBoList.size()<=5){
            avgDataBoList=dbAvgDataBoList;
        }else {
            int rowA=dbAvgDataBoList.size()/2;
            int row=rowA/2;
            int rowB=rowA-row;
            int rowC=rowA+row;
            avgDataBoList.add(dbAvgDataBoList.get(0));
            avgDataBoList.add(dbAvgDataBoList.get(rowB));
            avgDataBoList.add(dbAvgDataBoList.get(rowA));
            avgDataBoList.add(dbAvgDataBoList.get(rowC));
            avgDataBoList.add(dbAvgDataBoList.get(dbAvgDataBoList.size()-1));
        }
        AvgDataBo avgData=new AvgDataBo();
        avgData.setPv1(0d);
        avgData.setPv2(0d);
        avgData.setPv3(0d);
        avgData.setPv4(0d);
        avgData.setPv5(0d);
        avgData.setTime("xxxx 00:00:00");
        avgDataBoList.add(0,avgData);
        return avgDataBoList;
    }

    @Override
    public MyData startData(String pch) {
        LambdaQueryWrapper<MyData> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(MyData::getPch,pch);
        lambdaQueryWrapper.orderByAsc(MyData::getId).last("limit 1");
        return this.list(lambdaQueryWrapper).get(0);
    }

    @Override
    public MyData endData(String pch) {
        LambdaQueryWrapper<MyData> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(MyData::getPch,pch);
        lambdaQueryWrapper.orderByDesc(MyData::getId).last("limit 1");
        return this.list(lambdaQueryWrapper).get(0);
    }

    @Override
    public List<Categorize> categorizeList() {
        return this.baseMapper.categorizeList();
    }


    @Override
    public int clearTableData() {
        return this.baseMapper.clearTableData();
    }
}
